/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#ifndef XDRBASE_H
#define XDRBASE_H

#include <stdio.h>
#include <rpc/xdr.h>

#define HEADER_LEN (sizeof(struct header))
struct header {
    unsigned short reqcode;  /* request code */
    unsigned int length;     /* length of the data */
};

typedef struct keyvalue {
  unsigned int key;
  char *value;
} key_value_t;

#define REQUEST_KEY_OPTIONS                 1
#define REQUEST_KEY_RESREQ                  2
#define REQUEST_KEY_NUMBER                  3
#define REQUEST_KEY_NAMES                   4
#define REQUEST_KEY_CHECKUSER               5
#define REQUEST_KEY_CHECKHOST               6
#define REQUEST_KEY_JOBIDS                  7
#define REQUEST_KEY_QUEUENAME               8
#define REQUEST_KEY_USERNAME                9
#define REQUEST_KEY_JOBNAME                10
#define REQUEST_KEY_HOSTNAME               11
#define REQUEST_KEY_SIGNAL                 12
#define REQUEST_KEY_CHKPNT_DIR             13
#define REQUEST_KEY_CHKPNT_METHOD          14
#define REQUEST_KEY_CHKPNT_PERIOD          15
#define REQUEST_KEY_CHKPNT_FLAG            16
#define REQUEST_KEY_REQUEST_CODE           17
#define REQUEST_KEY_POSITION               18
#define REQUEST_KEY_SUBMIT_TYPE            19
#define REQUEST_KEY_SUBMIT_BEGINTIME       20
#define REQUEST_KEY_SUBMIT_TERMTIME        21
#define REQUEST_KEY_SUBMIT_PRECMD          22
#define REQUEST_KEY_SUBMIT_DEPENDENCY      23
#define REQUEST_KEY_SUBMIT_LOGINSHELL      24
#define REQUEST_KEY_SUBMIT_COMMAND         25
#define REQUEST_KEY_SUBMIT_NUM_XFILE       26
#define REQUEST_KEY_SUBMIT_XFILE           27
#define REQUEST_KEY_SUBMIT_ERRFILE         28
#define REQUEST_KEY_SUBMIT_INFILE          29
#define REQUEST_KEY_SUBMIT_OUTFILE         30
#define REQUEST_KEY_SUBMIT_THREAD_PERTASK  31
#define REQUEST_KEY_SUBMIT_JOBNAME         32
#define REQUEST_KEY_SUBMIT_NODE            33
#define REQUEST_KEY_SUBMIT_NUM_PROCESSOR   34
#define REQUEST_KEY_SUBMIT_MAX_PROCESSOR   35
#define REQUEST_KEY_SUBMIT_JOBGROUP        36
#define REQUEST_KEY_SUBMIT_PROJECT         37
#define REQUEST_KEY_SUBMIT_QUEUE           38
#define REQUEST_KEY_SUBMIT_RESREQ          39
#define REQUEST_KEY_SUBMIT_CPULIMIT        40
#define REQUEST_KEY_SUBMIT_RUNLIMIT        41
#define REQUEST_KEY_SUBMIT_DATALIMIT       42
#define REQUEST_KEY_SUBMIT_FILELIMIT       43
#define REQUEST_KEY_SUBMIT_CORELIMIT       44
#define REQUEST_KEY_SUBMIT_STACKLIMIT      45
#define REQUEST_KEY_SUBMIT_RSSLIMIT        46
#define REQUEST_KEY_SUBMIT_PROCESSLIMIT    47
#define REQUEST_KEY_SUBMIT_SWAPLIMIT       48
#define REQUEST_KEY_SUBMIT_NOFILELIMIT     49
#define REQUEST_KEY_SUBMIT_OPENMAXLIMIT    50
#define REQUEST_KEY_SUBMIT_USERGROUP       51
#define REQUEST_KEY_SUBMIT_JOBPRIORITY     52
#define REQUEST_KEY_SUBMIT_SIGNAL          53
#define REQUEST_KEY_SUBMIT_MAIL            54
#define REQUEST_KEY_MODIFY_COMMAND         55
#define REQUEST_KEY_MODIFY_JOBID           57
#define REQUEST_KEY_RESTART_CHKPNTDIR      58
#define REQUEST_KEY_SUBMIT_OPTIONS         59
#define REQUEST_KEY_SUBMIT_OPTIONS2        60
#define REQUEST_KEY_MODIFY_DELETE_OPTIONS  61
#define REQUEST_KEY_MODIFY_DELETE_OPTIONS2 62
#define REQUEST_KEY_SUBMIT_TIME            63
#define REQUEST_KEY_SUBMIT_FROMHOST        64
#define REQUEST_KEY_SUBMIT_UMASK           65
#define REQUEST_KEY_SUBMIT_HOMEDIR         66
#define REQUEST_KEY_SUBMIT_USERID          67
#define REQUEST_KEY_SUBMIT_USERNAME        68
#define REQUEST_KEY_SUBMIT_CWD             69
#define REQUEST_KEY_RESTART_JOBFILE        70
#define REQUEST_KEY_RESTART_JOBPID         71
#define REQUEST_KEY_SUBMIT_GPU_NUM         73
#define REQUEST_KEY_SUBMIT_GPU_MODEL       74
#define REQUEST_KEY_SUBMIT_GPU_MPS         75
#define REQUEST_KEY_SUBMIT_GPU_MEM         76
#define REQUEST_KEY_SUBMIT_GPU_EXCLUSIVE   77
#define REQUEST_KEY_JOBSTATUS              78
#define REQUEST_KEY_PENDREASON             79
#define REQUEST_KEY_JOBPID                 80
#define REQUEST_KEY_JOBPGID                81
#define REQUEST_KEY_EXECUTE_UID            82
#define REQUEST_KEY_EXECUTE_GID            83
#define REQUEST_KEY_EXECUTE_FLAG           84
#define REQUEST_KEY_EXECUTE_CWD            85
#define REQUEST_KEY_EXECUTE_HOME           86
#define REQUEST_KEY_NODE_RANK              87
#define REQUEST_KEY_CPUTIME                88
#define REQUEST_KEY_EXITSTATUS             89
#define REQUEST_KEY_NUMBER_NEED            91
#define REQUEST_KEY_FILECOPY_MODE          92
#define REQUEST_KEY_FILECOPY_UESR          93
#define REQUEST_KEY_FILECOPY_LFILE         94
#define REQUEST_KEY_FILECOPY_RFILE         95
#define REQUEST_KEY_FILECOPY_NUMBUF        96
#define REQUEST_KEY_FILECOPY_BUF           97
#define REQUEST_KEY_JOBGROUP_LIMIT         98
#define REQUEST_KEY_SUBMIT_POSTCMD         99
#define REQUEST_KEY_JOB_MESSAGE           100
#define REQUEST_KEY_HOST_MODEL            101
#define REQUEST_KEY_HOST_TYPE             102
#define REQUEST_KEY_RUN_WINDOW            103
#define REQUEST_KEY_DISPATCH_WINDOW       104
#define REQUEST_KEY_RESOURCE              105
#define REQUEST_KEY_MAXJOB                106
#define REQUEST_KEY_LOGLEVEL              108
#define REQUEST_KEY_LOGCLASS              109
#define REQUEST_KEY_LOGFILE               110
#define REQUEST_KEY_NODE_INDEX            111
#define REQUEST_KEY_NODE_STATUS           112
#define REQUEST_KEY_LOAD_SEQNO            113
#define REQUEST_KEY_CLUSTER_CHECKSUM      114
#define REQUEST_KEY_NUM_LOADINDEX         115
#define REQUEST_KEY_NUM_USER_LOADINDEX    116
#define REQUEST_KEY_NUM_LOAD_VALUE        117
#define REQUEST_KEY_NUM_RESOURCE          118
#define REQUEST_KEY_RESOURCE_VALUE        119
#define REQUEST_KEY_NUM_GPU               120
#define REQUEST_KEY_GPU_MEMORY            121
#define REQUEST_KEY_GPU_UT                122
#define REQUEST_KEY_GPU_MODE              123
#define REQUEST_KEY_GPU_TEMPERATURE       124
#define REQUEST_KEY_SUBMIT_REQUEUE_EXITCODE  125
#define REQUEST_KEY_JOB_REASON            126
#define REQUEST_KEY_JOB_SUBREASON         127
#define REQUEST_KEY_JOB_ACTPID            128
#define REQUEST_KEY_JOB_ACTSTATUS         129
#define REQUEST_KEY_GPU_STATUS            130
#define REQUEST_KEY_NUM_TASK              131
#define REQUEST_KEY_TASK_ARGC             132
#define REQUEST_KEY_TASK_ARGV             133
#define REQUEST_KEY_START_TIME            134
#define REQUEST_KEY_USAGE_MEM             135
#define REQUEST_KEY_USAGE_SWAP            136
#define REQUEST_KEY_USAGE_UTIME           137
#define REQUEST_KEY_USAGE_STIME           138
#define REQUEST_KEY_USAGE_NPID            139
#define REQUEST_KEY_USAGE_NPGID           140
#define REQUEST_KEY_USAGE_PIDINFO         141
#define REQUEST_KEY_USAGE_PGID            142
#define REQUEST_KEY_FAIL_REASON           143
#define REQUEST_KEY_END_TIME              144
#define REQUEST_KEY_FIRSTNODE_PORT        145
#define REQUEST_KEY_JOB_FILE              146
#define REQUEST_KEY_JOB_ERROR             147
#define REQUEST_KEY_EXECUTE_NUMNODE       148
#define REQUEST_KEY_JOB_ACTSTATE          149
#define REQUEST_KEY_JOB_ACTSIGNAL         150

struct request {
    unsigned int number;
    struct keyvalue *keyvalues;
};

/********** add for compile **********/
struct lenData {
    unsigned int len;
    char *data;
};

enum kind {
  CLIENT_AUTH,
};

struct auth {
    uid_t uid;
    uid_t gid;
    char *user_name;
    enum kind kind;
    union authBody {
        struct lenData token;
    } k;
};

extern bool_t gf_xdr_element(XDR *, void *, struct header *,
                             bool_t (*)(XDR *, void *, struct header *, void *), void *);
extern void gf_init_header(struct header *);
extern bool_t gf_xdr_message(XDR *, void *, struct header *, struct auth *,
                             bool_t (*)(XDR *, void *, struct header *, void *), void *);
extern unsigned int gf_xdrsize_auth(struct auth *);
extern bool_t gf_xdr_auth(XDR *, void *, struct header *, void *);
extern void gf_set_auth(struct auth *, char *);
extern bool_t gf_xdr_header (XDR *, struct header *);
extern unsigned int gf_xdrsize_request(struct request *);
extern bool_t gf_xdr_request(XDR *, void *, struct header *, void *);
extern bool_t gf_xdr_string(XDR *, void *, struct header *, void *);
extern bool_t gf_xdr_int(XDR *, void *, struct header *, void *);
extern bool_t gf_xdr_long(XDR *, void *, struct header *, void *);
extern bool_t gf_xdr_float(XDR *, void *, struct header *, void *);
extern void gf_xdr_free(bool_t (*)(XDR *, void *, struct header *, void *), void *);
extern void free_key_value(unsigned int, key_value_t **);

#endif
